package com.gxar.quick.ar.server.base.controller;

import com.google.common.collect.Lists;
import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.quick.ar.server.base.common.enums.order.OrderMethodEnum;
import com.gxar.quick.ar.server.base.facade.ExchangeItemFacade;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.quick.ar.server.base.model.exchange.vo.request.*;
import com.gxar.quick.ar.server.base.model.exchange.vo.response.*;
import com.gxar.quick.ar.server.base.model.user.exchange.vo.response.UserExchangeItemWorksResponse;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksTemplateExchangeRequest;
import com.gxar.quick.ar.server.db.service.ExchangeItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/8 15:41
 */
@Api(tags = "兑换品接口")
@RestController
@RequestMapping("/api/exchange-item")
@RequiredArgsConstructor
@Slf4j
public class ExchangeController {

    private final ExchangeItemService exchangeItemService;
    private final ExchangeItemFacade exchangeItemFacade;

    @ApiOperation("兑换")
    @PostMapping("/exchange")
    public Result<Boolean> exchange(@RequestBody @Valid WorksTemplateExchangeRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        if (Objects.isNull(userProfile)) {
            throw new APIRuntimeException(ResultCodeEnum.USER_NOT_EXIST);
        }
        exchangeItemFacade.exchange(userProfile, request.getCode());
        return Result.success("兑换成功", true);
    }

    @ApiOperation("兑换记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
    })
    @GetMapping("/record")
    public Result<PageResult<ExchangeItemRecordResponse>> exchangeRecord(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                                             @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {
        UserProfile userProfile = LoginUserThread.get();
        if (Objects.isNull(userProfile)) {
            throw new APIRuntimeException(ResultCodeEnum.USER_NOT_EXIST);
        }
        List<Integer> orderMethodEnum = Lists.newArrayList(OrderMethodEnum.EXCHANGE.getCode());
        return exchangeItemService.exchangeOrder(userProfile, pageNum, pageSize, orderMethodEnum);
    }

    @ApiOperation("订单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
    })
    @GetMapping("/order")
    public Result<PageResult<ExchangeItemRecordResponse>> exchangeOrder(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                                        @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {
        UserProfile userProfile = LoginUserThread.get();
        if (Objects.isNull(userProfile)) {
            throw new APIRuntimeException(ResultCodeEnum.USER_NOT_EXIST);
        }
        List<Integer> orderMethodEnum = Lists.newArrayList(OrderMethodEnum.EXCHANGE.getCode(), OrderMethodEnum.MARKET_BUT.getCode(),
                OrderMethodEnum.HANDSEL.getCode(), OrderMethodEnum.TRANSFER.getCode());
        return exchangeItemService.exchangeOrder(userProfile, pageNum, pageSize, orderMethodEnum);
    }

    @ApiOperation("我的AR列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
    })
    @GetMapping("")
    public Result<PageResult<ExchangeItemPageResponse>> exchangeItemPage(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                                         @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {
        UserProfile userProfile = LoginUserThread.get();
        if (Objects.isNull(userProfile)) {
            throw new APIRuntimeException(ResultCodeEnum.USER_NOT_EXIST);
        }
        return exchangeItemService.exchangeItemPage(userProfile, pageNum, pageSize);
    }

    @ApiOperation("我的AR详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_exchange_item_id", value = "用户兑换品ID", dataType = "number"),
    })
    @GetMapping("/{user_exchange_item_id}")
    public Result<ExchangeItemInfoResponse> exchangeItemInfo(@PathVariable("user_exchange_item_id") Long userExchangeItemId) {
        UserProfile userProfile = LoginUserThread.get();
        if (Objects.isNull(userProfile)) {
            throw new APIRuntimeException(ResultCodeEnum.USER_NOT_EXIST);
        }
        return exchangeItemService.exchangeItemInfo(userProfile, userExchangeItemId);
    }

    @ApiOperation("修改兑换品信息")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody @Valid ExchangeItemUpdateRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        exchangeItemService.update(userProfile, request);
        return Result.success("修改成功", true);
    }

    @ApiOperation("赠送")
    @PostMapping("/send")
    public Result<ExchangeItemSendResponse> send(@RequestBody @Valid ExchangeItemSendRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        ExchangeItemSendResponse response = exchangeItemService.send(userProfile, request);
        return Result.success(response);
    }

    @ApiOperation("转赠")
    @PostMapping("/transfer")
    public Result<Boolean> transfer(@RequestBody @Valid ExchangeItemTransferRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        exchangeItemService.transfer(userProfile, request);
        return Result.success("转赠成功", true);
    }

    @ApiOperation("兑换品作品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "兑换码", dataType = "string")
    })
    @GetMapping("/works")
    public Result<UserExchangeItemWorksResponse> userExchangeItemWorks(@RequestParam("code") String itemCode) {
        UserProfile userProfile = LoginUserThread.get();
        UserExchangeItemWorksResponse response = exchangeItemFacade.userExchangeItemWorks(userProfile.getId(), itemCode);
        return Result.success(response);
    }

    @ApiOperation("兑换品抽奖")
    @PostMapping("/lottery")
    public Result<ExchangeItemLotteryResponse> lottery(HttpServletRequest httpServletRequest, @RequestBody @Valid ExchangeItemLotteryRequest request) {
        String token = httpServletRequest.getHeader("qimen-token");
        return Result.success(exchangeItemFacade.lottery(LoginUserThread.get(), request.getCode(), token));
    }

    @ApiOperation("奖品核销")
    @PostMapping("/write-off")
    public Result<ExchangeItemWriteOffResponse> writeOff(HttpServletRequest httpServletRequest, @RequestBody @Valid ExchangeItemWriteOffRequest request) {
        String token = httpServletRequest.getHeader("qimen-token");
        return Result.success(exchangeItemFacade.writeOff(LoginUserThread.get(), request.getCode(), token));
    }

}
